﻿Imports System.Data.SqlClient

Public Class DBAccess
    Public Function GetSqlConnection() As SqlConnection
        Dim ConnectResult As SqlConnection
        Dim ConnectionString As String = GetConnectionString("Conn")

        If ConnectionString = "" Then
            ConnectResult = Nothing
        Else
            Dim Connection As New SqlConnection(ConnectionString)
            Try
                If Connection IsNot Nothing Then
                    Connection.Open()
                End If
                ConnectResult = Connection
            Catch Ex As System.Data.SqlClient.SqlException
                ConnectResult = Nothing
            Finally
                Connection.Close()
            End Try
        End If

        Return ConnectResult
    End Function


    Private Function GetConnectionString(ByVal ConnectionStringConfig As String) As String
        Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(ConnectionStringConfig)

        Dim ConnectionString As String
        If Not settings Is Nothing Then
            ConnectionString = settings.ConnectionString
        Else
            ConnectionString = ""
        End If

        Return ConnectionString
    End Function
End Class


